import { getMessageInfoAPI } from '@/api/message'
import { createSlice } from '@reduxjs/toolkit'

const messageStore = createSlice({
    name: 'message',
    initialState: {
        messageInfo: []
    },
    reducers: {
        setMessageInfo (state, action) {
            state.messageInfo = action.payload
        }
    }
})

const {setMessageInfo} = messageStore.actions
// 获取reducer函数
const messageReducer = messageStore.reducer

// 获取消息
const fetchMessageInfo = (id) => {
    return async (dispatch) => {
      const res = await getMessageInfoAPI(id)
      dispatch(setMessageInfo(res.data))
    }
  }

export { fetchMessageInfo }

export default messageReducer